﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmLeaveList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int LeaveID;
        LeaveClient wcfLeave;
        RoleRightInfo listRole;


        public override void Show_Form()
        {
            this.Show();
        }


        public frmLeaveList()
        {
            InitializeComponent();
            // LoadLanguage();

            wcfLeave = Service.Leave();
            listRole = UIParamater.Quyen_HinhThucNghi;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole._delete)
                btnDel.Enabled = false;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            BuildVoucherGrid();
            LoadData();
            this.KeyPreview = true;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_ContextMenuStrip(ref mnuPopup);
        }
        private void BuildVoucherGrid()
        {
            

            UISetting.AddColumn(ref dgvLeaveList, "Leave_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_IsSystem", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_BranchCode", "Đơn vị", "", 1, false, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_Code", "Mã", "", 60, true, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_Name", "Tên", "", 150, true, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_DateNumber", "Số ngày nghỉ", "R", 120, true, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_SalaryFactory", "Tỉ lệ hưởng lương", "R", 140, true, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_Status", "Được tính lương", "R", 120, true, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_MedicalInsurance", "Đóng BHYT", "R", 100, true, true);
            UISetting.AddColumn(ref dgvLeaveList, "Leave_SocialInsurance", "Đóng BHXH", "R", 100, true, true);
        }
        private void LoadData()
        {
            DataTable dt = wcfLeave.Leave_GetList(out itemCount, currPage, pageSize, Program.strBranchCode).Tables[0];
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();

            dgvLeaveList.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvLeaveList.Rows.Insert(i, 1);
                dgvLeaveList.Rows[i].Cells["Leave_ID"].Value = dt.Rows[i]["Leave_ID"].ToString();
                dgvLeaveList.Rows[i].Cells["Leave_IsSystem"].Value = dt.Rows[i]["Leave_IsSystem"].ToString();
                dgvLeaveList.Rows[i].Cells["Leave_BranchCode"].Value = dt.Rows[i]["Leave_BranchCode"].ToString();
                dgvLeaveList.Rows[i].Cells["Leave_Code"].Value = dt.Rows[i]["Leave_Code"].ToString();
                dgvLeaveList.Rows[i].Cells["Leave_Name"].Value = dt.Rows[i]["Leave_Name"].ToString();
                dgvLeaveList.Rows[i].Cells["Leave_DateNumber"].Value = Convert.ToInt32(dt.Rows[i]["Leave_DateNumber"]).ToString();
                dgvLeaveList.Rows[i].Cells["Leave_SalaryFactory"].Value = Convert.ToDecimal(dt.Rows[i]["Leave_SalaryFactory"]).ToString("#0.0#");
                dgvLeaveList.Rows[i].Cells["Leave_Status"].Value = Convert.ToBoolean(dt.Rows[i]["Leave_Status"]) == true ? "Có" : "Không";
                dgvLeaveList.Rows[i].Cells["Leave_MedicalInsurance"].Value = Convert.ToBoolean(dt.Rows[i]["Leave_MedicalInsurance"]) == true ? "Có" : "Không";
                dgvLeaveList.Rows[i].Cells["Leave_SocialInsurance"].Value = Convert.ToBoolean(dt.Rows[i]["Leave_SocialInsurance"]) == true ? "Có" : "Không";
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmLeaveInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvLeaveList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                LeaveID = Convert.ToInt32(dgvLeaveList.CurrentRow.Cells["Leave_ID"].Value);
                if (wcfLeave.Leave_Using(LeaveID) != 0)
                {
                    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfLeave.Leave_Delete(LeaveID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvLeaveList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    LeaveID = Convert.ToInt32(dgvLeaveList.CurrentRow.Cells["Leave_ID"].Value);
                    Form f = new frmLeaveInfo(LeaveID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;

            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;

            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage - 1;

            LoadData();
        }

        private void dgvLeaveList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmLeaveList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void frmLeaveList_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnNewPopUp_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditPopUp_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDelPopUp_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }

        private void dgvLeaveList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvLeaveList.SelectedRows.Count > 0)
                {
                    mnuPopup.Enabled = true;
                    btnNewPopUp.Enabled = true;
                    btnEditPopUp.Enabled = true;
                    btnDelPopUp.Enabled = true;
                }
            }
        }

        private void frmLeaveList_Activated(object sender, EventArgs e)
        {
            //LoadLanguage();
            if (dgvLeaveList.Columns.Count > 0)
            {
                //dgvLeaveList.Columns["Leave_Code"].HeaderText = UpdateUI.GetString("maso");
                //dgvLeaveList.Columns["Leave_Name"].HeaderText = UpdateUI.GetString("ten");
                //dgvLeaveList.Columns["Leave_Status"].HeaderText = UpdateUI.GetString("huongluong");
                //dgvLeaveList.Columns["Leave_DateNumber"].HeaderText = "Số ngày nghỉ";
                //dgvLeaveList.Columns["Leave_SalaryFactory"].HeaderText = UpdateUI.GetString("tyleluongnghi");
                //dgvLeaveList.Columns["Leave_MedicalInsurance"].HeaderText = UpdateUI.GetString("dongbhyte");
                //dgvLeaveList.Columns["Leave_SocialInsurance"].HeaderText = UpdateUI.GetString("dongbhxh");
            }
        }

        private void frmLeaveList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfLeave.Close();
        }

    }
}
